package com.tanguy.user.ui.main

import android.support.v4.view.ViewPager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.tanguy.user.R
import com.tanguy.user.base.LazyFragment
import com.tanguy.user.custom.FrescoImageLoader
import com.tanguy.user.ui.hotel.activity.PromotionActivity
import com.tanguy.user.ui.hotel.adapter.HotelOrderAdapter
import com.tanguy.user.utlis.extend.click
import com.youth.banner.Banner
import com.youth.banner.BannerConfig
import kotlinx.android.synthetic.main.fragment_hotel.*
import org.jetbrains.anko.find
import org.jetbrains.anko.support.v4.startActivity

/**
 * Created by dab on 2017/9/30 0030 11:24
 * 酒店
 */
class HotelFragment : LazyFragment() {
    override fun viewLayoutID(): Int = R.layout.fragment_hotel
    lateinit var banner: Banner
    lateinit var recyclerView: RecyclerView

    override fun onFirstVisibleToUser(view: View?) {
        initBanner(view)
        initRecyclerView(view)
        view?.apply {
            click(R.id.tv_promotion) { startActivity<PromotionActivity>() }//特价促销
        }
    }

    private fun initRecyclerView(view: View?) {
        view?.apply {
            recyclerView = find<RecyclerView>(R.id.rv_content)
            recyclerView.isNestedScrollingEnabled = false
            recyclerView.layoutManager = LinearLayoutManager(context)
            recyclerView.adapter = HotelOrderAdapter(arrayListOf(1, 2, 3, 4))
        }
    }


    override fun onVisibleOrInvisibleToUser(visible: Boolean) {
        super.onVisibleOrInvisibleToUser(visible)
        if (visible) {
            banner.start()
        } else {
            banner.stopAutoPlay()
            sr_refresh.isRefreshing = false
        }
    }

    /**
     * 修改指示器的样式
     */
    fun upIndicator(position: Int, linearLayout: LinearLayout) {
        val pos = position % linearLayout.childCount
        for (i in 0..linearLayout.childCount) {
            linearLayout.getChildAt(i)?.find<View>(R.id.view_select)?.visibility = if (pos == i) View.VISIBLE else View.GONE
            linearLayout.getChildAt(i)?.find<View>(R.id.view_no_select)?.visibility = if (pos == i) View.GONE else View.VISIBLE
        }
    }

    /**
     * 初始化banner相关
     */
    private fun initBanner(view: View?) {
        view!!.apply {
            banner = find<Banner>(R.id.banner).apply {
                setBannerStyle(BannerConfig.NOT_INDICATOR)
                setImageLoader(FrescoImageLoader())
                setDelayTime(5000)
            }
            val linearLayout = find<LinearLayout>(R.id.layout_indicator)
            val viewPager = banner.find<ViewPager>(com.youth.banner.R.id.bannerViewPager)
            viewPager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
                override fun onPageSelected(position: Int) {
                    upIndicator(position, linearLayout)
                }
            })
            val arrayListOf = arrayListOf(R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
            for (i in 0..arrayListOf.size - 1) {
                LayoutInflater.from(context).inflate(R.layout.view_banner_indicator, linearLayout)
            }
            banner.setImages(arrayListOf)
            banner.start()
        }
    }

}